package zhiziyibai.simpleweather.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import zhiziyibai.simpleweather.db.City;
import zhiziyibai.simpleweather.db.County;
import zhiziyibai.simpleweather.db.Province;
import zhiziyibai.simpleweather.gson.Weather;

/**
 * Created by 执子一白 on 2017/5/22.
 */

public class Utility {
    private static final String TAG = "Utility";
    /*将返回的JSON数据解析成Weather类*/
    public static Weather handleWeatherResponse(String response){
        try {
            //解析数据主体内容
            JSONObject jsonObject=new JSONObject(response);
            JSONArray jsonArray=jsonObject.getJSONArray("HeWeather");
            String weatherContent=jsonArray.getJSONObject(0).toString();
            //通过fromJson方法转换
            return new Gson().fromJson(weatherContent,Weather.class);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "handleWeatherResponse: "+e.getMessage());
        }
        return null;
    }
    /*解析和处理服务器返回的省级数据*/
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                //解析数据
                JSONArray allProvinces=new JSONArray(response);
                for (int i=0;i<allProvinces.length();i++) {
                    JSONObject provinceObject=allProvinces.getJSONObject(i);
                    //将数据组装成实体类对象
                    Province province=new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "handleProvinceResponse: "+e.getMessage());
            }
        }
        return false;
    }
    /*解析和处理服务器返回的市级数据*/
    public  static boolean handleCityResponse(String response,int provinceId)
    {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities=new JSONArray(response);
                for (int i=0;i<allCities.length();i++) {
                    JSONObject cityObject=allCities.getJSONObject(i);
                    City city=new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "handleCityResponse: "+e.getMessage());
            }
        }
        return false;
    }
    /*解析和处理服务器返回的县级数据*/
    public static boolean handleCountyResponse(String response,int cityId)
    {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties=new JSONArray(response);
                for (int i=0;i<allCounties.length();i++) {
                    JSONObject countyObject=allCounties.getJSONObject(i);
                    County county=new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "error"+e.getMessage()+":handleCountyResponse");
            }
        }
        return false;
    }
}
